var app = require('../app'),
    loading = require('../../modules/loading'),
    i=0,
    lock=false,
    lockfas = false;
toastr.options.timeOut = 3000;

//案件分配
var arrcaseid = [];
//解析地址栏
function GetQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}
var entry_record_id='';
i=0;
lockfas = false;
$('body')
.on('click','.delentry', function(){
    entry_record_id = $(this).attr('data-entry_record_id');
})
.on('click','#importCaseSubmit', function(){
    if(lockfas)return;
    lockfas = true;
    var _this = $(this);
    var file = document.getElementById("upfile").files[0];
    if(!file){
        toastr.error('请上传文档');
        return false;
    }
    var formData = new FormData();//这里需要实例化一个FormData来进行文件上传
    formData.append("file",document.getElementById("upfile").files[0]);
    formData.append("data",$("#importCase_form").serialize());
    loading.showLoading({
        type: 1,
        tip: '加载中...'
    })
    $.ajax({
        url:"/publiccaseimport/upload",
        method:"post",
        data: formData,
        processData : false,
        contentType : false,
        success:function(res){
            console.log(res);
            if(res.code == 200){
                loading && loading.hideLoading();
                toastr.success(res.message);
                setTimeout(function(){
                    document.location.reload();
                },1500)
                lockfas = false;

            }else{
                loading && loading.hideLoading();
                toastr.error(res.message);
            }
            $(this).attr('disabled',false);
            lockfas = false;

        }
    })
    lockfas = false;
    loading && loading.hideLoading();
    return false;
})

//上传文档
.on('change','.uploadFile',function(e){
    let files = $(this)[0].files[0];
    let name = files.name;
    $(this).prev().text(name);
})
.on('click','#import_submit', function(){
    $("#recordId").val($(this).attr('data-id'));
    $("#chean_num").text($(this).attr('data-num'));
    $("#case_submit").show();
})
.on('click','.distribution', function(){
    if(lockfas)return;
    lockfas = true;
    $.ajax({
        url:"/publiccaseimport/caseSubmit",
        method:"post",
        data:{
            recordId: $("#recordId").val(),
        },
        success:function(res){
            if(res.code==200){
                toastr.success(res.message);
                setTimeout(function(){
                    window.location.href = window.location.href;
                },1500)
            }else{
                toastr.error(res.message);
            }
            lockfas = false;
        },
        error: function(err){
            lockfas = false;
            toastr.error('网络错误,请稍后重试');
        }
    })
})
